package com.cn.onetrip.view;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ViewSwitcher.ViewFactory;

import com.cn.onetrip.activity.R;
import com.cn.onetrip.objects.AdvertsObj;
import com.cn.onetrip.untility.SysApplication;

public class AdView extends RelativeLayout implements ViewFactory {

	private Context context;
	private ImageSwitcher imageSwitcher;
	private ImageButton adCancl;

	private final int SWITCH_TIME = 5000;
	private final int START_TIME = 10000;

	private Timer switchTimer;
	private TimerTask switchTask;
	private Timer startTimer;
	private TimerTask startTask;
	private List<AdvertsObj> advertsList;
	private int adSize = 0;

	public AdView(Context context) {
		super(context);
		this.context = context;
	}

	public AdView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();

		imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
		adCancl = (ImageButton) findViewById(R.id.deleteAd);

		advertsList = SysApplication.advertsList;
		if (imageSwitcher != null && advertsList.size() != 0) {

			imageSwitcher.setFactory(this);
			adCancl.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					stopAdPlay();
				}
			});

		}
	}

	private void startAdTimer() {
		setViewVisibility(false);
		if (switchTimer != null) {
			switchTimer.cancel();
			switchTimer = null;
		}

		if (SysApplication.adCache.size() > 0) {
			startTimer = new Timer();
			startTask = new TimerTask() {
				public void run() {
					Message message = new Message();
					message.what = 0;
					mhandler.sendMessage(message);
				}
			};

			startTimer.schedule(startTask, START_TIME, START_TIME);
		}
	}

	private void swtichAdTimer() {
		setViewVisibility(true);

		if (SysApplication.adCache.size() > 0) {
			switchTimer = new Timer();
			switchTask = new TimerTask() {
				public void run() {
					Message message = new Message();
					message.what = 1;
					mhandler.sendMessage(message);
				}
			};

			switchTimer.schedule(switchTask, SWITCH_TIME, SWITCH_TIME);
		}
	}

	private Handler mhandler = new Handler() {
		public void dispatchMessage(android.os.Message msg) {
			if (msg.what == 0) {
				startTimer.cancel();
				startTimer = null;
				swtichAdTimer();
			} else if (msg.what == 1) {
				adSize++;
				if (adSize > advertsList.size() - 1) {
					adSize = 0;
					startAdTimer();
				} else {
					imageSwitcher.setImageDrawable(SysApplication.adCache
							.get(advertsList.get(adSize).advert_icon));
				}
			}
		};
	};

	private void setViewVisibility(boolean visible) {

		if (visible && SysApplication.adCache.size() > 0) {
			imageSwitcher.setImageDrawable(SysApplication.adCache
					.get(advertsList.get(0).advert_icon));
			this.setVisibility(View.VISIBLE);
			imageSwitcher.setVisibility(View.VISIBLE);
			adCancl.setVisibility(View.VISIBLE);
		} else {
			this.setVisibility(View.INVISIBLE);
			imageSwitcher.setVisibility(View.GONE);
			adCancl.setVisibility(View.GONE);
		}
	}

	public void stopAdPlay() {
		stopTimer();

		if (advertsList.size() > 0) {
			setViewVisibility(false);
			startAdTimer();
			adSize = 0;
		}
	}

	public void startTimer() {
		if (advertsList.size() > 0)
			swtichAdTimer();
		else
			this.setVisibility(View.GONE);
	}

	public void stopTimer() {
		if (switchTimer != null) {
			switchTimer.cancel();
			switchTimer = null;
		}

		if (startTimer != null) {
			startTimer.cancel();
			startTimer = null;
		}
	}

	@Override
	public View makeView() {
		ImageView imageView = new ImageView(this.getContext());
		imageView.setScaleType(ImageView.ScaleType.FIT_XY);
		imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		imageView.setImageDrawable(SysApplication.adCache.get(advertsList
				.get(0).advert_icon));
		return imageView;
	}
}
